//
//  SecondViewController.m
//  XDCommonApp
//
//  Created by XD-XY on 2/12/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "SecondViewController.h"
#import "orderDetailViewController.h"

#import "MyOrderCell.h"

#import <AssetsLibrary/AssetsLibrary.h>

#define  fenyeNumber  10;
@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    isGetMore = NO;
    _curpage = 1;
    [self initFavData:1];
    
//    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"isBackToOrderListToRefresh"] isEqualToString:@"1"]) {
//        _curpage = 1;
//        [self initFavData:1];
//        [[NSUserDefaults standardUserDefaults]setObject:@"0" forKey:@"isBackToOrderListToRefresh"];
//    }
    if ([XDTabBarViewController sharedXDTabBarViewController].reminder_IV.isHidden==NO){
//        _curpage = 1;
//        [self initFavData:1];
        [XDTabBarViewController sharedXDTabBarViewController].reminder_IV.hidden = YES;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.titleLabel.text = @"我的订单";
    self.leftBtn.hidden = YES;
    self.contentView.backgroundColor = UIColorFromRGB(CONTENTVIEW_COLOR);

    
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT-UI_TAB_BAR_HEIGHT-UI_NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
    myTableView.dataSource = self;
    myTableView.delegate = self;
    myTableView.showsVerticalScrollIndicator = NO;
    myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    myTableView.backgroundColor = [UIColor clearColor];
    [myTableView addFooterWithTarget:self action:@selector(footerPulltoRefreshData)];
    [myTableView addHeaderWithTarget:self action:@selector(heaererPulltoRefreshData)];

    [self.contentView addSubview:myTableView];
    
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 15)];
    header.backgroundColor = UIColorFromRGB(0xfffefa);
    myTableView.tableHeaderView = header;
    
    noDatasView = [[UIImageView alloc] initWithFrame:CGRectMake((UI_SCREEN_WIDTH-111)/2, (UI_MAINSCREEN_HEIGHT - UI_NAVIGATION_BAR_HEIGHT - 78)/2, 111, 78)];
    noDatasView.image = [UIImage imageNamed:@"noData"];
    [self.contentView addSubview:noDatasView];
    noDatasView.hidden = YES;
//    _curpage = 1;
    _ListArray = [[NSMutableArray alloc]init];
//    [self initFavData:_curpage];
}



#pragma mark - TableView DataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_ListArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 131+15;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellIde = @"cell";
    MyOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
    if (!cell) {
        cell = [[MyOrderCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = UIColorFromRGB(0xffffff);
    
    orderListModel * orderModel = [_ListArray objectAtIndex:indexPath.row];
    
    cell.emplLB.text = [NSString stringWithFormat:@"摄影师: %@",orderModel.EmplName];
    
    [cell.headIV sd_setImageWithURL:[NSURL URLWithString:orderModel.PhotoID] placeholderImage:nil];
//    [cell.headIV sd_setImageWithURL:[NSURL URLWithString:orderModel.EmplAvator] placeholderImage:nil];
    
    cell.titleLB.text= orderModel.SeriesName;
    
    if (IS_NOT_EMPTY(orderModel.Onecost)) {
        if (IS_NOT_EMPTY(orderModel.Twoecost)) {
          cell.priceLB.text =[NSString stringWithFormat:@"¥%@+%@",orderModel.Onecost,orderModel.Twoecost];
        }else{
         cell.priceLB.text =[NSString stringWithFormat:@"¥%@",orderModel.Onecost];
        }
    }else{
          cell.priceLB.text =[NSString stringWithFormat:@"¥%@",orderModel.OrderCost];
    }
    
    cell.pTimeLB.text = [NSString stringWithFormat:@"拍摄时间: %@",orderModel.ShootDate];
    
    if ([orderModel.Status isEqualToString:@"未付款"] || [orderModel.Status isEqualToString:@"等待确认"]) {
        cell.statusLB.textColor = UIColorFromRGB(0xf26d5f);
    }else if ([orderModel.Status isEqualToString:@"已取消"]){
        cell.statusLB.textColor = UIColorFromRGB(0x66cccc);
    }else{
       cell.statusLB.textColor = UIColorFromRGB(0xa8d842);
    }

    cell.statusLB.text = orderModel.Status;
    cell.xTimeLB.text = [NSString stringWithFormat:@"下单时间: %@",orderModel.CreateTime];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    orderDetailViewController *orderDetail = [[orderDetailViewController alloc]init];
    
    ///存入 单例 刷新订单状态
    [XDTools sharedXDTools].orderDetailVC = orderDetail;
    
    orderListModel *orderModel = [_ListArray objectAtIndex:indexPath.row];
    orderDetail.orderID = orderModel.OrderID;
    [self.navigationController pushViewController:orderDetail animated:YES];
}

- (void)changeStatus:(UIButton *)sender{
//    if (sender.tag == 871200) {
//        sender.backgroundColor = [UIColor redColor];
//    }else if (sender.tag == 871201) {
//        sender.backgroundColor = [UIColor yellowColor];
//    }else{
//        sender.backgroundColor = [UIColor orangeColor];
//    }
}
#pragma mark  =======上下拉刷新========
-(void)footerPulltoRefreshData
{
    isGetMore = YES;
    if (_curpage < totalPage) {
        _curpage++;
        [self initFavData:_curpage];
    }else{
        if (_curpage == totalPage) {
             myTableView.footerRefreshingText = @"没有更多订单...";
             [self performSelector:@selector(delayToEndRefreshLoading) withObject:nil afterDelay:.8f];
        }
    }
}
-(void)heaererPulltoRefreshData
{
    _curpage = 1;
    [self initFavData:_curpage];
    isGetMore = NO;
}
-(void)delayToEndRefreshLoading
{
   [myTableView footerEndRefreshing];
    myTableView.footerRefreshingText = @"正在加载数据";
}

-(void)initFavData:(int)curPage
{
    noDatasView.hidden = YES;
    //http://123.57.16.119/index.php?m=Api&c=Customer&a=getCustomerOrder&CustomerID=''
    
    if(![XDTools NetworkReachable]){
        _curpage = _curpage==1?(1):(_curpage-1);
        [XDTools showTips:brokenNetwork toView:self.contentView];
        return;
    }
    //xD8179395FF064708AC2B64E17E166B26x
    NSDictionary *requestDic = @{@"CustomerID":[[NSUserDefaults standardUserDefaults] objectForKey:kMY_USER_ID],@"page":[NSString stringWithFormat:@"%d",_curpage],@"pageSize":@10};
    
    ASIHTTPRequest *request = [XDTools getRequestWithDict:requestDic API:API_CUSTOMER_ORDER_LIST];
    __weak ASIHTTPRequest * mrequest = request;
        [request setCompletionBlock:^{
            
            [myTableView footerEndRefreshing];
            [myTableView headerEndRefreshing];
            
            NSDictionary *resultDic = [XDTools JSonFromString:[mrequest responseString]];
            DDLOG(@"res....%@",resultDic);
            if ([[resultDic objectForKey:@"status"] intValue]==1) {
//                [XDTools showTips:[requestDic objectForKey:@"msg"] toView:self.contentView];
                if ([[resultDic allKeys]containsObject:@"data"]) {
                    if (!isGetMore) {
                        [_ListArray removeAllObjects];
                    }
                    
                    for (int i = 0; i< [[[resultDic objectForKey:@"data"] objectForKey:@"list"]count]; i++) {
                        NSDictionary *addDic = [[[resultDic objectForKey:@"data"] objectForKey:@"list"]objectAtIndex:i];
                        orderListModel *addorderModel = [[orderListModel alloc]initWithDictionary:addDic];
                        [_ListArray addObject:addorderModel];
                    }

                    [myTableView reloadData];
                    
                    if ([resultDic[@"data"][@"List"][@"count"] intValue]%10==0) {
                        totalPage = [[[resultDic objectForKey:@"data"] objectForKey:@"count"] intValue]/10;
                    }else{
                        totalPage = [[[resultDic objectForKey:@"data"] objectForKey:@"count"] intValue]/10+1;
                    }
                    if (_ListArray.count) {
                        noDatasView.hidden = YES;
                    }else{
                        noDatasView.hidden = NO;
                    }
                }else{
                    if (_ListArray.count) {
                        [_ListArray removeAllObjects];
                        [myTableView reloadData];
                    }
                    noDatasView.hidden = NO;
                    [XDTools showTips:[resultDic objectForKey:@"msg"] toView:self.view];
                    
                }
                
            }else{
                _curpage = _curpage==1?(1):(_curpage-1);
                [XDTools showTips:[resultDic objectForKey:@"msg"] toView:self.view];
                [myTableView footerEndRefreshing];
                [myTableView headerEndRefreshing];
            }
            
        }];
        [request setFailedBlock:^{
            _curpage = _curpage==1?(1):(_curpage-1);
            [myTableView headerEndRefreshing];
            [myTableView footerEndRefreshing];
            [XDTools hideProgress:self.contentView];
            if (mrequest.error.code == 2) {
                [XDTools showTips:@"网络请求超时" toView:self.view];

            }
        }];
    [request setTimeOutSeconds:12];
    [request startAsynchronous];
 
}
//WFK=>未付款,
//YFK=>已付款,
//WFKZDQX=>未付款自动取消,
//WFKCSQX=>未付款超时取消,
//YFKQX=>已付款取消)
//StatPS:拍摄状态(YCF=>已出发,KSPS=>开始拍摄,JSPS=>拍摄结束)
//StatXP:选片状态(YSC=>已上传,YXW=>已选完) StatSH:收货状态(YFH=>已发货)
//-(NSString *)returnButtonString:(NSString *)buttonTypeString
//{
//    if ([buttonTypeString isEqualToString:@"WFK"]) { //未付款 1
//        return @"未付款";
//    }else if ([buttonTypeString isEqualToString:@"YFK"]){//已付款2
//        return @"等待拍摄";
//    }else if ([buttonTypeString isEqualToString:@"YFKQX"]){//已付款取消3
//        return @"已取消";
//    }else if ([buttonTypeString isEqualToString:@"WFKZDQX"]){//未付款自动取消4
//        return  @"已取消";
//    }else if ([buttonTypeString isEqualToString:@"WFKCSQX"]){//未付款超时取消5
//        return @"已取消";
//    }else if ([buttonTypeString isEqualToString:@"YCF"]){//已出发6
//        return @"摄影师出门";
//    }else if ([buttonTypeString isEqualToString:@"KSPS"]){//开始拍摄7
//        return @"拍摄中";
//    }else if ([buttonTypeString isEqualToString:@"JSPS"]){//结束拍摄8
//        return @"拍摄完成";
//    }else if ([buttonTypeString isEqualToString:@"YSC"]){//已上传9
//        return  @"已上传";
//    }else if ([buttonTypeString isEqualToString:@"YXW"]){//已选完－如果没有评价－显示待评价10
//        return  @"选片完成";
//    }else if ([buttonTypeString isEqualToString:@"YSH"]){//已收货11－如果有评价交易完成－没有显示待评价10
//        return  @"已收货";
//    }else if ([buttonTypeString isEqualToString:@"YFH"]){//已发货12
//        return  @"已发货";
//    }else if ([buttonTypeString isEqualToString:@"DDP"]){//待点评13
//        return  @"待评价";
//    }else if ([buttonTypeString isEqualToString:@"YWC"]){//已完成－－如果没有评价－显示待评价14
//        return @"交易完成";
//    }
//    return  nil;
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
